from datetime import datetime
import bts.index
import bts.search
import bts.core
from demo import models

topic = models.Topic(name='My Topic')
topic.put()
for i in range(0, 1):
    post = models.BlogPost(topic=topic, title='My Blog Entry Title', body='Lorem ipsum dolor sit amet conseqtetur etc')
    post.save()
    bts.index.update_model(post)
    post.title = 'My Blog Entry Heading'
    post.save()
    bts.index.update_model(post)
    if i in [0, 9]:
        letters = 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z'.split(',')
        start_time = datetime.now()
        for i in range(0, len(letters)):
            start_letter = letters[i]
            end_letter = None
            if start_letter != 'z':
                end_letter = letters[i+1]
            bts.index.merge_string_updates(start_letter, end_letter)
        bts.index.merge_updates('term_reference')
        bts.index.merge_updates('term_integer')
        bts.index.merge_updates('term_bool')
        bts.index.merge_updates('term_date')
        bts.index.merge_updates('term_datetime')
        print "Merged updates in %s" % (datetime.now() - start_time)

start_time = datetime.now()
query = bts.search.query(models.BlogPost).filter('body =', bts.core.stemmer.stemWord('lorem'))
results = query.fetch(30)
print "Fetched %s of %s in %s" % (len(results), query.hit_count, datetime.now() - start_time)